The reliance on the modulo operator allows continuous index movement, but it creates a classic implementation challenge: ambiguity.
- Specifically, the empty state and the full state can both result in the condition front == rear.
- To resolve this ambiguity, we employ the One-Empty-Slot Rule. We intentionally restrict the maximum number of elements we store to MAX_SIZE - 1.
- This creates a clear, distinct separation between the two boundary conditions:
- Empty State Condition: The queue is empty when `front` equals `rear`.
$$front = rear$$
- Full State Condition: The queue is full when the index immediately following `rear` is equal to `front`. This preserves the mandatory empty slot.
$$(rear + 1) \ \% \ MAX\_SIZE = front$$
- By sacrificing one slot, we ensure that whenever front == rear, the queue is guaranteed to be empty.
Boundary Conditions Summary
| State | Condition | Example ($MAX\_SIZE=5$) |
|---|---|---|
| Empty | $front == rear$ | `front=0, rear=0` |
| Full | $(rear + 1) \% MAX\_SIZE == front$ | `front=0, rear=4` |